package com.logic.landseaserver.ws;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.domain.Coupon;
import com.logic.landseaserver.service.IAuthorizationCodeService;
import com.logic.landseaserver.ws.dto.CouponDTO;
import com.logic.landseaserver.ws.dto.CouponGeneralDTO;
import com.logic.landseaserver.ws.dto.CouponPersonalDTO;
import com.logic.landseaserver.ws.request.CouponPutRecordReq;
import com.logic.landseaserver.ws.request.CustomerConditionReq;
import com.logic.landseaserver.ws.response.CouponPutRecordResp;
import com.logic.landseaserver.ws.response.CouponUsedResp;
import com.logic.landseaserver.ws.response.TenantDetailResp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：AuthorizationCodeRest</br>
 * 类描述：授权码的接口</br>
 * 创建人：Crystal</br>
 * 创建时间：2017年9月6日 下午1:58:43</br>
 * @version 1.0
 *
 */
@Path("/authorizationCode")
@Api(value = "authorizationCode")
public class AuthorizationCodeRest
{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private IAuthorizationCodeService iauthorizationCodeService;
    
    @POST
    @Path("/authorizedItems")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取授权项目", position = 4)
    public Response getAuthorizedItems()
    {
        CommonResultMap map = new CommonResultMap();
        Map<String, Object> respMap  = null;
        try
        {
            respMap = iauthorizationCodeService.getAuthorizedItem();
            map.setData(respMap);
        }
        catch (Exception e)
        {
            logger.error("getAuthorizedItem error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/authorizedFee")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取授权费项", position = 4)
    public Response getAuthorizedFee()
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Map<String,Object> resultResps = iauthorizationCodeService.getAuthorizedFees();
            map.setData(resultResps);
        }
        catch (Exception e)
        {
            logger.error("getAuthorizedItem error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    

    
    @POST
    @Path("/tenantDetail")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "根据手机号获取租客姓名和房间号", position = 2)
    public Response TenantDetail(
        @ApiParam(value = "param", required = true) CustomerConditionReq conditionReq)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            TenantDetailResp resp = iauthorizationCodeService.queryTenantDetail(conditionReq);
            map.setData(resp);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (Exception e)
        {
            logger.error("resp error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    

    @GET
    @Path("/queryCoupon/{couponId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "query Coupon", position = 2)
    public Response queryCoupon(
        @ApiParam(value = "param", required = true) @PathParam("couponId") Integer id)
    {
        CommonResultMap map = new CommonResultMap();
        CouponDTO coupon = new CouponDTO();
        try
        {
            coupon = iauthorizationCodeService.queryCoupon(id);
            map.setData(coupon);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (Exception e)
        {
            logger.error("list error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/saveCoupon")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "新增以及保存授权码", position = 1)
    public Response saveCoupon(@ApiParam(value = "param", required = true) Coupon coupon)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            Integer id = coupon.getId();
            if (null == id || 0 == id)
            {
                iauthorizationCodeService.insertCoupon(coupon);
                map.setResult(CommonResult.SUCCESS, "create success");
            }
            else
            {
                iauthorizationCodeService.editCoupon(coupon);
                map.setResult(CommonResult.SUCCESS, "edit coupon success");
            }           
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(logger, "editCoupon", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    @POST
    @Path("/deleteCoupon")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "logic delete coupon by set deleted", position = 3)
    public Response logicCouponDelete(@ApiParam(value = "param", required = true) @QueryParam("id") Integer id)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            iauthorizationCodeService.deleteCoupon(id);
            map.setResult(CommonResult.SUCCESS, "delete Coupon");
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(logger, "deleteCoupon", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    
    
    @POST
    @Path("/queryPersonCouponList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取个人优惠券列表")
    public Response queryPersonCouponList(
        @ApiParam(value = "param", required = false)
        QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<CouponPersonalDTO> couponList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            couponList = iauthorizationCodeService.queryPersonByPage(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(couponList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("queryCouponList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    
    @POST
    @Path("/queryGeneralCouponList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取通用优惠券列表")
    public Response queryGeneralCouponList(
        @ApiParam(value = "param", required = false)
        QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<CouponGeneralDTO> couponList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            couponList = iauthorizationCodeService.queryGeneralByPage(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(couponList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("queryCouponList error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    //投放记录
    @POST
    @Path("/queryCouponPutRecord")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "query Coupon", position = 2)
    public Response queryCouponPutRecord(
        @ApiParam(value = "param", required = false)
        QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        List<CouponPutRecordResp> coupons = new ArrayList<CouponPutRecordResp>();
        try
        {
            queryUtil.getPagingTool().calculateStartIndex();
            coupons = iauthorizationCodeService.queryCouponPutRecord(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setResultTotalCount(totalCount);
            map.setData(coupons);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (Exception e)
        {
            logger.error("list error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    
    //使用记录
    @POST
    @Path("/queryCouponUsedrecord")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "query Coupon", position = 2)
    public Response queryCouponUsedrecord(
        @ApiParam(value = "param", required = false) QueryUtil queryUtil)
    {
        CommonResultMap map = new CommonResultMap();
        List<CouponUsedResp> coupons = new ArrayList<CouponUsedResp>();
        try
        {
            queryUtil.getPagingTool().calculateStartIndex();
            coupons = iauthorizationCodeService.queryCouponUsedrecord(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setResultTotalCount(totalCount);
            map.setData(coupons);
            map.setResult(CommonResult.SUCCESS);
        }
        catch (Exception e)
        {
            logger.error("list error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    //投放
    @POST
    @Path("/saveCouponPutRecord")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "投放授权码", position = 1)
    public Response saveCouponPutRecord(@ApiParam(value = "param", required = true) CouponPutRecordReq req)
    {
        CommonResultMap map = new CommonResultMap();
        try
        {
            iauthorizationCodeService.insertCouponPutRecord(req);
            map.setResult(CommonResult.SUCCESS, "create success");         
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(logger, "saveCouponPutRecord", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    //微信端显示通用优惠券
    @POST
    @Path("/weChatAuthCodeList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "微信端显示授权码", position = 4)
    public Response getAuthCodeList()
    {
        CommonResultMap map = new CommonResultMap();
        Map<String, Object> respMap  = null;
        try
        {
            respMap = iauthorizationCodeService.getAuthCodeList();
            map.setData(respMap);
        }
        catch (Exception e)
        {
            logger.error("getAuthorizedItem error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
